package com.mengma.ec.controller.backend;

import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ControllerCommons;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.Product;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.IProductService;
import com.mengma.ec.service.portal.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.rmi.server.ServerCloneException;

/**
 * @Auther: xixi
 * @Date: 2018/9/19 09:21
 * @Description:  产品
 */

@RestController
@RequestMapping(value = "/manage/product/")
public class ProductController {

    @Autowired
    private IProductService iProductService;


    // 新增OR更新产品
    @PostMapping("saveOrUpdata.do")
    public ServerResponse<String> saveOrUpdata(@RequestBody(required = true) Product product, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("请先登录！");
        }
        if(user.getRole().intValue() != Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");
        }
        ServerResponse response = iProductService.saveOrUpdata(product);
        return response;
    }

    //产品list
    @PostMapping("list.do")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               HttpSession session){
        ServerResponse loginResponse= ControllerCommons.checkLogin(session);
        if (!loginResponse.isSuccess()){
            return ServerResponse.createByErrorMessage("未登录！");
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if(user.getRole().intValue() == Const.Role.ROLE_ADMIN){
            ServerResponse response = iProductService.FindAllListData(pageNum,pageSize,session);
            return response;
        }
        return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");
    }

    @PostMapping("search.do")
    public ServerResponse search(@RequestParam String productName,
                                 @RequestParam Integer productId,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 HttpSession session){
        ServerResponse loginResponse= ControllerCommons.checkLogin(session);
        if (!loginResponse.isSuccess()){
            return ServerResponse.createByErrorMessage("未登录！");
        }

        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if(user.getRole().intValue() == Const.Role.ROLE_ADMIN){
            ServerResponse response = iProductService.search(productName,productId,pageNum,pageSize,session);
            return response;
        }
        return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");

    }
}
